package edu.unindra.elearning.form;

import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.SwingWorker;

import usu.widget.ButtonGlass;
import usu.widget.Form;
import usu.widget.GlassPane;
import edu.unindra.elearning.db.ResourceDatabase;
import edu.unindra.elearning.form.addin.ELearningFormMainAdmin;
import edu.unindra.elearning.form.addin.ELearningFormMainAdminMateri;
import edu.unindra.elearning.form.addin.ELearningFormMainAdminMateriAdd;
import edu.unindra.elearning.form.addin.ELearningFormMainAdminMateriEdit;
import edu.unindra.elearning.form.addin.ELearningFormMainAdminOperator;
import edu.unindra.elearning.form.addin.ELearningFormMainAdminOperatorAdd;
import edu.unindra.elearning.form.addin.ELearningFormMainAdminOperatorEdit;
import edu.unindra.elearning.form.addin.ElearningFormMainAdminReport;
import edu.unindra.elearning.model.DefaultMateri;
import edu.unindra.elearning.model.DefaultOperator;
import edu.unindra.elearning.model.Materi;
import edu.unindra.elearning.model.Operator;
import edu.unindra.elearning.toolbar.ToolbarBlackGray;
import edu.unindra.elearning.util.About;
import edu.unindra.elearning.util.PanelBackGround;

import java.awt.AWTException;
import java.awt.CardLayout;
import java.awt.Toolkit;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import usu.widget.constan.Animation;
import usu.widget.util.WidgetUtilities;

import java.awt.Dimension;

public class ELearningFormAdmin extends Form {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2471410853205547369L;

	private ExecutorService executor;
	private Toolkit toolkit;
	private GlassPane glassPane;
	private PanelBackGround mainPanel;
	private JPanel mainSubPanel;

	private ToolbarBlackGray toolBar;
	private JLabel titleBody;
	private About about;
	private ButtonGlass btnHome;

	private String s;
	private String t;
	private int i;
	private boolean b;

	private ELearningFormMainAdmin eLearningFormMain;
	private ELearningFormMainAdminOperator eLearningFormMainAdminOperator;
	private ELearningFormMainAdminOperatorAdd eLearningFormMainAdminOperatorAdd;
	private ELearningFormMainAdminOperatorEdit eLearningFormMainAdminOperatorEdit;
	private ElearningFormMainAdminReport eLearningFormMainAdminReport;
	private ELearningFormMainAdminMateri eLearningFormMainAdminMateri;
	private ELearningFormMainAdminMateriAdd eLearningFormMainAdminMateriAdd;
	private ELearningFormMainAdminMateriEdit eLearningFormMainAdminMateriEdit;

	/** Creates new form ELearningForm */
	public ELearningFormAdmin() {
		setResizable(false);
		executor = Executors.newCachedThreadPool();
		toolkit = Toolkit.getDefaultToolkit();

		getContentPane().setEnabled(false);
		setAnimationShow(Animation.SHOW_FROM_BOTTOM);
		setAnimationHide(Animation.HIDE_TO_BOTTOM);
		setLocationRelativeTo(null);
		setIconImage(Toolkit
				.getDefaultToolkit()
				.getImage(
						ELearningFormAdmin.class
								.getResource("/edu/unindra/elearning/image/network.png")));
		setTitle("Admin");
		setMinimumSize(new Dimension(750, 640));

		java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit()
				.getScreenSize();
		setBounds((screenSize.width - 800) / 2, (screenSize.height - 600) / 2,
				800, 600);

		addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowClosing(java.awt.event.WindowEvent evt) {
				formWindowClosing(evt);
			}
		});

		initComponents();

		getRootPane().getInputMap().put(
				KeyStroke.getKeyStroke((char) KeyEvent.VK_ESCAPE), "CLOSE");
		getRootPane().getActionMap().put("CLOSE", new AbstractAction() {

			private static final long serialVersionUID = -1;

			public void actionPerformed(ActionEvent e) {
				((GlassPane) getGlassPane()).hideComponent();
			}
		});

		initActions();
	}

	private void initComponents() {
		glassPane = new usu.widget.GlassPane();

		mainPanel = new PanelBackGround();
		mainSubPanel = new JPanel();
		eLearningFormMain = new ELearningFormMainAdmin();
		eLearningFormMain.getBtnSoal().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		eLearningFormMainAdminOperator = new ELearningFormMainAdminOperator();
		eLearningFormMainAdminOperatorAdd = new ELearningFormMainAdminOperatorAdd();
		eLearningFormMainAdminOperatorEdit = new ELearningFormMainAdminOperatorEdit();

		eLearningFormMainAdminReport = new ElearningFormMainAdminReport();

		eLearningFormMainAdminMateri = new ELearningFormMainAdminMateri();
		eLearningFormMainAdminMateriAdd = new ELearningFormMainAdminMateriAdd();
		eLearningFormMainAdminMateriEdit = new ELearningFormMainAdminMateriEdit();

		eLearningFormMain.getBtnSoal().setText("Materi");
		eLearningFormMain.getBtnReport().setText("Reports");
		eLearningFormMain.getBtnOperator().setText("Operator");
		about = new About();
		about.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				aboutActionPerformed(evt);
			}
		});

		toolBar = new ToolbarBlackGray();
		toolBar.setRollover(true);
		toolBar.setToolTipText("Tool Bar");

		btnHome = new ButtonGlass();
		btnHome.setFont(new Font("Tahoma", Font.BOLD, 11));
		btnHome.setForeground(new java.awt.Color(255, 255, 255));
		btnHome.setIcon(new javax.swing.ImageIcon(getClass().getResource(
				"/edu/unindra/elearning/image/Home 2.png"))); // NOI18N
		btnHome.setMnemonic('M');
		btnHome.setText("Menu");
		btnHome.setToolTipText("Klik untuk kembali ke menu utama");
		btnHome.setFocusable(false);
		btnHome.setRoundRect(true);
		btnHome.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
		btnHome.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				buttonHomeActionPerformed(evt);
			}
		});
		toolBar.add(btnHome);

		titleBody = new JLabel();
		titleBody.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
		titleBody.setForeground(new java.awt.Color(255, 255, 255));
		titleBody.setText("E'Learning");

		mainSubPanel.setOpaque(false);
		mainSubPanel.setLayout(new java.awt.CardLayout());
		mainSubPanel.add(eLearningFormMain, "HOME");
		mainSubPanel.add(eLearningFormMainAdminOperator, "OPERATOR");
		mainSubPanel.add(eLearningFormMainAdminReport, "REPORT");
		mainSubPanel.add(eLearningFormMainAdminMateri, "MATERI");

		setGlassPane(glassPane);
		glassPane.setAlphaDisable(10);
		glassPane.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER,
				100, 100));

		javax.swing.GroupLayout panelUtamaLayout = new javax.swing.GroupLayout(
				mainPanel);
		mainPanel.setLayout(panelUtamaLayout);
		panelUtamaLayout
				.setHorizontalGroup(panelUtamaLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								panelUtamaLayout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												panelUtamaLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
																mainSubPanel,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																764,
																Short.MAX_VALUE)
														.addComponent(titleBody))
										.addContainerGap()));
		panelUtamaLayout
				.setVerticalGroup(panelUtamaLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								panelUtamaLayout
										.createSequentialGroup()
										.addContainerGap()
										.addComponent(titleBody)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
										.addComponent(
												mainSubPanel,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												451, Short.MAX_VALUE)
										.addContainerGap()));

		getContentPane().add(mainPanel, java.awt.BorderLayout.CENTER);
		getContentPane().add(toolBar, java.awt.BorderLayout.PAGE_START);

	}

	private void initActions() {
		eLearningFormMain.getBtnOperator().addActionListener(
				new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						if (eLearningFormMainAdminOperator.isVisible()) {
							return;
						}

						new LoadOperator().execute();
						showPanel(eLearningFormMainAdminOperator, "OPERATOR");
						setTitleBody("Operator");
					}
				});

		eLearningFormMainAdminOperator.getBtnAdd().addActionListener(
				new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						showComponentInGlasspane(eLearningFormMainAdminOperatorAdd);
					}
				});

		eLearningFormMainAdminOperator.getBtnEdit().addActionListener(
				new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						buttonEditElearningFormMainAdminOperator(e);
					}
				});

		eLearningFormMainAdminOperator.getBtnDelete().addActionListener(
				new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						buttonDeleteElearningFormMianAdminOperator(e);
					}
				});

		eLearningFormMainAdminOperator.getBtnRefresh().addActionListener(
				new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						new LoadOperator().execute();
					}
				});

		eLearningFormMainAdminOperatorAdd.getBtnSubmit().addActionListener(
				new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						buttonSubmitAddOperator(e);
					}
				});
		eLearningFormMainAdminOperatorAdd.getBtnCancel().addActionListener(
				new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						hideComponentFromGlasspane(e);
						eLearningFormMainAdminOperatorAdd.reset();
					}
				});

		eLearningFormMainAdminOperatorEdit.getBtnSubmit().addActionListener(
				new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						buttonSubmitEditOperator(e);
					}
				});
		eLearningFormMainAdminOperatorEdit.getBtnCancel().addActionListener(
				new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						hideComponentFromGlasspane(e);
					}
				});

		eLearningFormMain.getBtnReport().addActionListener(
				new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						if (eLearningFormMainAdminReport.isVisible()) {
							return;
						}

						new LoadOperator().execute();
						showPanel(eLearningFormMainAdminReport, "REPORT");
						setTitleBody("Report");
					}
				});

		eLearningFormMain.getBtnSoal().addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if (eLearningFormMainAdminMateri.isVisible()) {
					return;
				}

				new LoadMateri().execute();
				showPanel(eLearningFormMainAdminMateri, "MATERI");
				setTitleBody("Materi");
			}
		});

		eLearningFormMainAdminMateri.getBtnAdd().addActionListener(
				new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						showComponentInGlasspane(eLearningFormMainAdminMateriAdd);
					}
				});

		eLearningFormMainAdminMateri.getBtnEdit().addActionListener(
				new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						buttonEditElearningFormMainAdminMateri(e);
					}
				});

		eLearningFormMainAdminMateri.getBtnDelete().addActionListener(
				new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						buttonDeleteElearningFormMianAdminMateri(e);
					}
				});

		eLearningFormMainAdminMateri.getBtnRefresh().addActionListener(
				new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						new LoadMateri().execute();
					}
				});

		eLearningFormMainAdminMateriAdd.getBtnSubmit().addActionListener(
				new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						buttonSubmitAddMateri(e);
					}
				});

		eLearningFormMainAdminMateriAdd.getBtnCancel().addActionListener(
				new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						hideComponentFromGlasspane(e);
						eLearningFormMainAdminMateriAdd.reset();
					}
				});
		eLearningFormMainAdminMateriEdit.getBtnSubmit().addActionListener(
				new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						buttonSubmitEditMateri(e);
					}
				});
		eLearningFormMainAdminMateriEdit.getBtnCancel().addActionListener(
				new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						hideComponentFromGlasspane(e);
					}
				});
	}

	protected void buttonDeleteElearningFormMianAdminOperator(ActionEvent e) {
		i = eLearningFormMainAdminOperator.getTable().getSelectedRowCount();

		if (i == 0) {
			return;
		}

		if (i > 1) {
			if (JOptionPane.showConfirmDialog(this,
					"Anda yakin akan menghapus " + i + " operator?",
					"Hapus Operator", JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION) {
				b = true;
			} else {
				b = false;
			}
		} else {
			if (JOptionPane.showConfirmDialog(this,
					"Anda yakin akan menghapus operator yang terseleksi?",
					"Hapus Operator", JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION) {
				b = true;
			} else {
				b = false;
			}
		}

		if (b) {
			WidgetUtilities.invokeLater(new Runnable() {

				@Override
				public void run() {
					while (eLearningFormMainAdminOperator.getTable()
							.getSelectedRowCount() > 0) {
						i = eLearningFormMainAdminOperator.getTable()
								.convertRowIndexToModel(
										eLearningFormMainAdminOperator
												.getTable().getSelectionModel()
												.getMaxSelectionIndex());

						s = eLearningFormMainAdminOperator.getModelTable()
								.getOperator(i).getId();

						try {
							@SuppressWarnings("unused")
							int retVal = ResourceDatabase.getInstance()
									.deleteOperator(s);
						} catch (Exception e) {
							e.printStackTrace();
						}

						eLearningFormMainAdminOperator.getModelTable()
								.removeOperator(i);
					}

					eLearningFormMainAdminOperator.getModelTable()
							.fireTableDataChanged();
					eLearningFormMainAdminOperator.getTable().revalidate();
				}
			});
		}

	}

	protected void buttonDeleteElearningFormMianAdminMateri(ActionEvent e) {
		i = eLearningFormMainAdminMateri.getTable().getSelectedRowCount();

		if (i == 0) {
			return;
		}

		if (i > 1) {
			if (JOptionPane.showConfirmDialog(this,
					"Anda yakin akan menghapus " + i + " materi?",
					"Hapus Materi", JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION) {
				b = true;
			} else {
				b = false;
			}
		} else {
			if (JOptionPane.showConfirmDialog(this,
					"Anda yakin akan menghapus materi yang terseleksi?",
					"Hapus Materi", JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION) {
				b = true;
			} else {
				b = false;
			}
		}

		if (b) {
			WidgetUtilities.invokeLater(new Runnable() {

				@Override
				public void run() {
					while (eLearningFormMainAdminMateri.getTable()
							.getSelectedRowCount() > 0) {
						i = eLearningFormMainAdminMateri.getTable()
								.convertRowIndexToModel(
										eLearningFormMainAdminMateri.getTable()
												.getSelectionModel()
												.getMaxSelectionIndex());

						s = eLearningFormMainAdminMateri.getModelTable()
								.getMateri(i).getMateri();
						t = eLearningFormMainAdminMateri.getModelTable()
								.getMateri(i).getKelas();
						try {
							@SuppressWarnings("unused")
							int retVal = ResourceDatabase.getInstance()
									.deleteMateri(s, t);
						} catch (Exception e) {
							e.printStackTrace();
						}

						eLearningFormMainAdminMateri.getModelTable()
								.removeMateri(i);
					}

					eLearningFormMainAdminMateri.getModelTable()
							.fireTableDataChanged();
					eLearningFormMainAdminMateri.getTable().revalidate();
				}
			});
		}

	}

	protected void buttonEditElearningFormMainAdminOperator(ActionEvent e) {
		if (eLearningFormMainAdminOperator.getTable().getSelectedRowCount() == 0) {
			return;
		}

		i = eLearningFormMainAdminOperator.getTable().convertRowIndexToModel(
				eLearningFormMainAdminOperator.getTable().getSelectedRow());
		Operator o = eLearningFormMainAdminOperator.getModelTable()
				.getOperator(i);
		eLearningFormMainAdminOperatorEdit.setOperator(o);
		showComponentInGlasspane(eLearningFormMainAdminOperatorEdit);
	}

	protected void buttonEditElearningFormMainAdminMateri(ActionEvent e) {
		if (eLearningFormMainAdminMateri.getTable().getSelectedRowCount() == 0) {
			return;
		}

		i = eLearningFormMainAdminMateri.getTable().convertRowIndexToModel(
				eLearningFormMainAdminMateri.getTable().getSelectedRow());
		Materi m = eLearningFormMainAdminMateri.getModelTable().getMateri(i);
		eLearningFormMainAdminMateriEdit.setMateri(m);
		showComponentInGlasspane(eLearningFormMainAdminMateriEdit);
	}

	private void aboutActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_aboutActionPerformed
		((GlassPane) getGlassPane()).hideComponent();
	}

	private void buttonHomeActionPerformed(java.awt.event.ActionEvent evt) {
		if (eLearningFormMain.isVisible()) {
			return;
		}
		showTransition(eLearningFormMain);
		setTitleBody(null);
		((CardLayout) mainSubPanel.getLayout()).show(mainSubPanel, "HOME");

	}

	private void showTransition(JComponent com) {
		try {
			((GlassPane) getGlassPane()).startTransition(com);
		} catch (AWTException ex) {
		}
	}

	public void showPanel(JPanel panel, String card) {
		if (panel.isVisible()) {
			return;
		} else {
			showTransition(mainPanel);
			((CardLayout) mainSubPanel.getLayout()).show(mainSubPanel, card);
		}
	}

	private void formWindowClosing(java.awt.event.WindowEvent evt) {// GEN-FIRST:event_formWindowClosing
		exit();
	}

	@Override
	public void exit() {
		if (showConfirm("Anda Yakin?") != JOptionPane.OK_OPTION) {
			return;
		}
		executor.shutdown();
		super.exit();
	}

	private int showConfirm(String message) {
		return JOptionPane.showConfirmDialog(this, message, "Konfirmasi",
				JOptionPane.OK_CANCEL_OPTION);
	}

	public void setTitleBody(String title) {
		if (title == null) {
			titleBody.setText("e'Learning");
		} else {
			titleBody.setText("e'Learning - " + title);
		}
	}

	private void hideComponentFromGlasspane(ActionEvent e) {
		((GlassPane) getGlassPane()).hideComponent();
	}

	private void showComponentInGlasspane(JComponent comp) {
		((GlassPane) getGlassPane()).showComponent(comp);
	}

	private void buttonSubmitAddOperator(ActionEvent e) {
		List<String> list = new LinkedList<String>();

		if (eLearningFormMainAdminOperatorAdd.isOperatorValid()) {
			Operator o = eLearningFormMainAdminOperatorAdd.getOperator();
			list.add(o.getId());
			list.add(o.getPassword());
			list.add(o.getName());
			list.add(o.getAddress());
			list.add(o.getContact());

			int retVal = 0;
			try {
				retVal = ResourceDatabase.getInstance().insertOperator(list);
			} catch (Exception e1) {
				e1.printStackTrace();
			}

			if (retVal > -1) {
				JOptionPane.showMessageDialog(this,
						"Proses penambahan operator baru berhasil",
						"Information", JOptionPane.INFORMATION_MESSAGE);

				eLearningFormMainAdminOperator.getModelTable().addOperator(o);
				((GlassPane) getGlassPane()).hideComponent();
				eLearningFormMainAdminOperatorAdd.reset();
			} else {
				JOptionPane.showMessageDialog(this,
						"Process Input Data Problem", "Error",
						JOptionPane.ERROR_MESSAGE);
			}

		} else {
			toolkit.beep();
			WidgetUtilities.showErrorMessage(this,
					eLearningFormMainAdminOperatorAdd.getInvalidMessage());
		}
	}

	private void buttonSubmitAddMateri(ActionEvent e) {
		List<String> list = new LinkedList<String>();

		if (eLearningFormMainAdminMateriAdd.isMateriValid()) {
			Materi m = eLearningFormMainAdminMateriAdd.getMateri();
			list.add(m.getMateri());
			list.add(m.getKelas());
			list.add(m.getPath());
			list.add(m.getStatus());

			int retVal = 0;
			try {
				retVal = ResourceDatabase.getInstance().insertMateri(list);
			} catch (Exception e1) {
				e1.printStackTrace();
			}

			if (retVal > -1) {
				JOptionPane.showMessageDialog(this,
						"Proses penambahan materi baru berhasil",
						"Information", JOptionPane.INFORMATION_MESSAGE);

				eLearningFormMainAdminMateri.getModelTable().addMateri(m);
				((GlassPane) getGlassPane()).hideComponent();
				eLearningFormMainAdminMateriAdd.reset();
			} else {
				JOptionPane.showMessageDialog(this,
						"Process Input Data Problem", "Error",
						JOptionPane.ERROR_MESSAGE);
			}

		} else {
			toolkit.beep();
			WidgetUtilities.showErrorMessage(this,
					eLearningFormMainAdminOperatorAdd.getInvalidMessage());
		}
	}

	protected void buttonSubmitEditOperator(ActionEvent e) {
		List<String> list = new LinkedList<String>();

		if (eLearningFormMainAdminOperatorEdit.isOperatorValid()) {
			Operator o = eLearningFormMainAdminOperatorEdit.getOperator();
			list.add(o.getId());
			list.add(o.getPassword());
			list.add(o.getName());
			list.add(o.getAddress());
			list.add(o.getContact());

			int retVal = 0;
			try {
				retVal = ResourceDatabase.getInstance().updateOperator(list);
			} catch (Exception e1) {
				e1.printStackTrace();
			}

			if (retVal > -1) {
				JOptionPane.showMessageDialog(this,
						"Proses perubahan data operator berhasil",
						"Information", JOptionPane.INFORMATION_MESSAGE);

				i = eLearningFormMainAdminOperator.getTable()
						.convertRowIndexToModel(
								eLearningFormMainAdminOperator.getTable()
										.getSelectedRow());
				eLearningFormMainAdminOperator.getModelTable().setOperatorAt(o,
						i);
				((GlassPane) getGlassPane()).hideComponent();
			} else {
				JOptionPane.showMessageDialog(this,
						"Process Perubahan Data Problem", "Error",
						JOptionPane.ERROR_MESSAGE);
			}

		} else {
			toolkit.beep();
			WidgetUtilities.showErrorMessage(this,
					eLearningFormMainAdminOperatorEdit.getInvalidMessage());
		}
	}

	protected void buttonSubmitEditMateri(ActionEvent e) {
		List<String> list = new LinkedList<String>();

		if (eLearningFormMainAdminMateriEdit.isMateriValid()) {
			Materi m = eLearningFormMainAdminMateriEdit.getMateri();
			list.add(m.getMateri());
			list.add(m.getKelas());
			list.add(m.getPath());
			list.add(m.getStatus());

			int retVal = 0;
			try {
				retVal = ResourceDatabase.getInstance().updateMateri(list);
			} catch (Exception e1) {
				e1.printStackTrace();
			}

			if (retVal > -1) {
				JOptionPane.showMessageDialog(this,
						"Proses perubahan data materi berhasil", "Information",
						JOptionPane.INFORMATION_MESSAGE);

				i = eLearningFormMainAdminMateri.getTable()
						.convertRowIndexToModel(
								eLearningFormMainAdminMateri.getTable()
										.getSelectedRow());
				eLearningFormMainAdminMateri.getModelTable().setMateriAt(m, i);
				((GlassPane) getGlassPane()).hideComponent();
			} else {
				JOptionPane.showMessageDialog(this,
						"Process Perubahan Data Problem", "Error",
						JOptionPane.ERROR_MESSAGE);
			}

		} else {
			toolkit.beep();
			WidgetUtilities.showErrorMessage(this,
					eLearningFormMainAdminMateriEdit.getInvalidMessage());
		}
	}

	public class LoadOperator extends SwingWorker<List<Operator>, String> {

		private DefaultOperator op;

		public LoadOperator() {
			eLearningFormMainAdminOperator.getBtnRefresh().setEnabled(false);
			eLearningFormMainAdminOperator.getBtnAdd().setEnabled(false);
			eLearningFormMainAdminOperator.getBtnDelete().setEnabled(false);
			eLearningFormMainAdminOperator.getBtnEdit().setEnabled(false);
			eLearningFormMainAdminOperator.getTable().setEnabled(false);
			eLearningFormMainAdminOperator.getModelTable().removeAllElements();
		}

		@Override
		protected List<Operator> doInBackground() throws Exception {
			ArrayList<Operator> array = new ArrayList<Operator>();

			List<Object[]> list = ResourceDatabase.getInstance().getOperator();
			for (int i = 0; i < list.size(); i++) {
				op = new DefaultOperator();

				op.setId(list.get(i)[0].toString());
				op.setPassword(list.get(i)[1].toString());
				op.setName(list.get(i)[2].toString());
				op.setAddress(list.get(i)[3].toString());
				op.setContact(list.get(i)[4].toString());

				array.add(op);
			}
			return array;
		}

		@Override
		protected void done() {
			executor.execute(new Runnable() {

				public void run() {
					try {
						eLearningFormMainAdminOperator.getModelTable().add(
								get());
					} catch (InterruptedException ex) {
						ex.getStackTrace();
					} catch (ExecutionException ex) {
						ex.getStackTrace();
					} finally {
						eLearningFormMainAdminOperator.getTable().revalidate();
						eLearningFormMainAdminOperator.getTable().repaint();
						eLearningFormMainAdminOperator.getBtnRefresh()
								.setEnabled(true);
						eLearningFormMainAdminOperator.getBtnAdd().setEnabled(
								true);
						eLearningFormMainAdminOperator.getBtnDelete()
								.setEnabled(true);
						eLearningFormMainAdminOperator.getBtnEdit().setEnabled(
								true);
						eLearningFormMainAdminOperator.getTable().setEnabled(
								true);
					}
				}
			});
		}
	}

	public class LoadMateri extends SwingWorker<List<Materi>, String> {

		private DefaultMateri mt;

		public LoadMateri() {
			eLearningFormMainAdminMateri.getBtnRefresh().setEnabled(false);
			eLearningFormMainAdminMateri.getBtnAdd().setEnabled(false);
			eLearningFormMainAdminMateri.getBtnDelete().setEnabled(false);
			eLearningFormMainAdminMateri.getBtnEdit().setEnabled(false);
			eLearningFormMainAdminMateri.getTable().setEnabled(false);
			eLearningFormMainAdminMateri.getModelTable().removeAllElements();
		}

		@Override
		protected List<Materi> doInBackground() throws Exception {
			ArrayList<Materi> array = new ArrayList<Materi>();

			List<Object[]> list = ResourceDatabase.getInstance().getMateri();
			for (int i = 0; i < list.size(); i++) {
				mt = new DefaultMateri();
				mt.setMateri(list.get(i)[0].toString());
				mt.setKelas(list.get(i)[1].toString());
				mt.setPath(list.get(i)[2].toString());
				mt.setStatus(list.get(i)[3].toString());

				array.add(mt);
			}
			return array;
		}

		@Override
		protected void done() {
			executor.execute(new Runnable() {

				public void run() {
					try {
						eLearningFormMainAdminMateri.getModelTable().add(get());
					} catch (InterruptedException ex) {
						ex.getStackTrace();
					} catch (ExecutionException ex) {
						ex.getStackTrace();
					} finally {
						eLearningFormMainAdminMateri.getTable().revalidate();
						eLearningFormMainAdminMateri.getTable().repaint();
						eLearningFormMainAdminMateri.getBtnRefresh()
								.setEnabled(true);
						eLearningFormMainAdminMateri.getBtnAdd().setEnabled(
								true);
						eLearningFormMainAdminMateri.getBtnDelete().setEnabled(
								true);
						eLearningFormMainAdminMateri.getBtnEdit().setEnabled(
								true);
						eLearningFormMainAdminMateri.getTable()
								.setEnabled(true);
					}
				}
			});
		}

	}

}
